﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class CX_PGZJTJControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        DropDownList DropDownList1;
        CustomGridView CustomGridList1;
        TextBox 立项号, 概算评估机构;
        Label lbXmNum, lbdzje, lbhzje, lbye, Label3,
            Label4, Label5, Label6, Label1, Label2;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack) {
                QSDataDLL();
                Bind();
            }
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            Button btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(Go_Search);
        }
        protected void InitControl() 
        {
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            立项号 = (TextBox)FindControl("立项号");
            概算评估机构 = (TextBox)FindControl("概算评估机构");

            lbXmNum = (Label)FindControl("lbXmNum");
            lbdzje = (Label)FindControl("lbdzje");
            lbhzje = (Label)FindControl("lbhzje");
            lbye = (Label)FindControl("lbye");
            Label3 = (Label)FindControl("Label3");

            Label4 = (Label)FindControl("Label4");
            Label5 = (Label)FindControl("Label5");
            Label6 = (Label)FindControl("Label6");
            Label1 = (Label)FindControl("Label1");
            Label2 = (Label)FindControl("Label2");
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        public void Bind()
        {
            DataTable dt = _EluZSDataProvider.GetPGZJTJ("0", getWhere());
            string xmNum = (_EluZSDataProvider.GetPGZJTJ("1", getWhere())).Rows.Count.ToString();
            if (dt.Rows.Count > 0)
            {
                List<string> lis = new List<string>();
                lis.Add("总户数");
                lis.Add("居民户数");
                lis.Add("工企单位户数");
                lis.Add("征收建筑总面积");
                lis.Add("评估建筑总面积");
                lis.Add("土地使用权总面积");
                lis.Add("概算补偿总金额");
                _EluZSDataProvider.AddCollectRow(dt, "编号", lis);
            }
            if (dt.Rows.Count > 0)
            {
                lbXmNum.Text = "项目总数为：" + xmNum + "个  ";
                lbdzje.Text = "征收建筑总面积：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["征收建筑总面积"]) + " m2  ";
                lbhzje.Text = "评估建筑总面积：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["评估建筑总面积"]) + " m2  ";
                lbye.Text = "土地使用权总面积：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["土地使用权总面积"]) + " m2  ";
                Label3.Text = "总户数：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["总户数"]) + " 户  ";
                Label4.Text = "居民户数：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["居民户数"]) + " 户  ";
                Label5.Text = "工企单位数：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["工企单位户数"]) + " 个  ";
                Label6.Text = "概算补偿总金额：" + string.Format("{0:N}", dt.Rows[dt.Rows.Count - 1]["概算补偿总金额"]) + " 元  ";
                string tt1 = dt.Rows[dt.Rows.Count - 1]["征收建筑总面积"].ToString();
                string tt2 = dt.Rows[dt.Rows.Count - 1]["概算补偿总金额"].ToString();
                Double t1 = 0; Double t2 = 0; int tcount = 0;
                if (!string.IsNullOrEmpty(xmNum)) { tcount = Convert.ToInt32(xmNum); }
                if (!string.IsNullOrEmpty(tt1)) { t1 = Convert.ToDouble(tt1); }
                if (!string.IsNullOrEmpty(tt2)) { t2 = Convert.ToDouble(tt2); }
                Double temp1 = Convert.ToDouble(t1 / tcount);
                Double temp2 = Convert.ToDouble(t2 / tcount);
                Label1.Text = "房屋征收建筑平均面积：" + string.Format("{0:N}", temp1) + " m2 ";
                Label2.Text = "征收评估总金额平均：" + string.Format("{0:N}", temp2) + " 元 ";
                dt.Rows.RemoveAt(dt.Rows.Count - 1);
            }
            else
            {
                lbXmNum.Text = "项目总数为：0个  ";
                lbdzje.Text = "征收建筑总面积：0 m2  ";
                lbhzje.Text = "评估建筑总面积：0 m2  ";
                lbye.Text = "土地使用权总面积：0 m2  ";
                Label3.Text = "总户数：0 户  ";
                Label4.Text = "居民户数：0 户  ";
                Label5.Text = "工企单位数：0 个  ";
                Label6.Text = "概算补偿总金额：0 元  ";
                Label1.Text = "房屋征收建筑平均面积：0 m2 ";
                Label2.Text = "征收评估总金额平均：0 元 ";
            }
            if (dt.Columns.Contains("PID")) { dt.Columns.Remove("PID"); }
            if (dt.Columns.Contains("ID")) { dt.Columns.Remove("ID"); }
            if (dt.Columns.Contains("SYNC_TYPE")) { dt.Columns.Remove("SYNC_TYPE"); }
            if (dt.Columns.Contains("CAN_SYNC")) { dt.Columns.Remove("CAN_SYNC"); }
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        private string getWhere()
        {
            StringBuilder sbWhere = new StringBuilder();
            string strLxh = 立项号.Text;
            string strQS = DropDownList1.SelectedValue;
            string strJG = 概算评估机构.Text;
            if (!string.IsNullOrEmpty(strLxh)) { sbWhere.AppendFormat(" and p1.立项号 like '%{0}%'", strLxh); }
            if (!string.IsNullOrEmpty(strQS)) { sbWhere.AppendFormat(" and p1.区属='{0}'", strQS); }
            if (!string.IsNullOrEmpty(strJG)) { sbWhere.AppendFormat(" and p1.概算评估机构 like '%{0}%'", strJG); }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sbWhere.AppendFormat(@" and  p1.区属 in(select keyname from zs_params where name='区属'and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%{0}%') Like '%'||keyname||'%')", userid);
            }
            return sbWhere.ToString();
        }

        protected void QSDataDLL()
        {
            //List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("区属");
            //EluZSProvider.Params item = new EluZSProvider.Params();
            //_params.Insert(0, item);

           
            DataTable dt = _EluZSDataProvider.GetParamYB("区属", GetSqlWhere());
            DropDownList1.DataSource = dt;
            DropDownList1.DataValueField = "keyname";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "");
        }

        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%' ";
            }
            return sqlWhere;
        }
    }
}


